package cn.yeziji.file.service.impl;

import cn.yeziji.file.service.InteractionService;
import cn.yeziji.file.vo.FileDataVO;
import cn.yeziji.forum.basic.file.WebUploaderFileEntity;
import cn.yeziji.forum.common.ForumPageResult;
import cn.yeziji.forum.dto.file.ForumFilePagingDTO;
import cn.yeziji.forum.entity.ForumFileEntity;
import cn.yeziji.forum.entity.ForumUserEntity;
import cn.yeziji.forum.service.ProvideService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 与论坛服务交互逻辑实现层
 *
 * @author gzkemays
 * @since 2022/5/16 15:17
 */
@Component
public class InteractionServiceImpl implements InteractionService {
  @Reference ProvideService provideService;
  @Resource HttpServletRequest request;

  @Override
  public ForumUserEntity getUserEntityByToken() {
    return provideService.getUserEntityByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
  }

  @Override
  public boolean checkToken(String token) {
    return provideService.checkToken(token);
  }

  @Override
  public Integer getProcess(String md5) {
    return provideService.getProcess(md5);
  }

  @Override
  public void save(WebUploaderFileEntity wfe) {
    provideService.save(wfe);
  }

  @Override
  public String getUsernameByToken() {
    return provideService.getUsernameByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
  }

  @Override
  public List<ForumFileEntity> getFileEntitiesBySource() {
    return provideService.getFileEntitiesBySource(getUsernameByToken());
  }

  @Override
  public List<ForumFileEntity> getFileEntitiesByScope(String scope) {
    return provideService.getFileEntitiesByScope(scope);
  }

  @Override
  public ForumPageResult<FileDataVO> getFileEntitiesPage(ForumFilePagingDTO pagingDTO) {
    ForumPageResult<ForumFileEntity> entitiesPage = provideService.getFileEntitiesPage(pagingDTO);
    // 数据转换
    ForumPageResult<FileDataVO> dataVos = new ForumPageResult<>();
    dataVos.setPage(entitiesPage.getPage());
    dataVos.setLimit(entitiesPage.getLimit());
    dataVos.setTotal(entitiesPage.getTotal());
    dataVos.setCount(entitiesPage.getCount());
    dataVos.setTotalPage(entitiesPage.getTotalPage());
    dataVos.setData(FileDataVO.buildByEntities(entitiesPage.getData()));
    return dataVos;
  }

  @Override
  public void del(String scope) {
    String username = getUsernameByToken();
    provideService.del(scope, username);
  }
}
